$(document).ready(function () {

    if (method != 'checkout') {
        // Custom combox
        $('.dropdownized').dropdownized();
    }

    if (method == 'details') {
        // Lightbox
        $('a.lightbox').lightBox({
            overlayBgColor:'#000',
            overlayOpacity:0.6,
            imageLoading:base_url + 'assets/img/lightbox/loading.gif',
            imageBtnClose:base_url + 'assets/img/lightbox/close.gif',
            imageBtnPrev:base_url + 'assets/img/lightbox/prev.gif',
            imageBtnNext:base_url + 'assets/img/lightbox/next.gif',
            imageBlank:base_url + 'assets/img/lightbox/blank.gif',
            containerResizeSpeed:350,
            txtImage:'Image',
            txtOf:'of'
        });

        var selected_date;

        /***********************************************************
         * JQUERY UI Calendar with event
         ***********************************************************/
        // get the current date
        var date = new Date();
        var m = date.getMonth(),
        d = date.getDate() + 1,
        y = date.getFullYear();

        function cal_init() {
            $('#exc_date').datepicker({
                minDate:new Date(y, m, d),
                dateFormat:'dd.mm.yy',
                inline:true,
                numberOfMonths:1,
                onSelect:function (dateStr) {
                    selected_date = $.datepicker.parseDate('dd.mm.yy', dateStr);
                },
                beforeShowDay:enableDaysOfWeek,
                showOn:'both',
                buttonImage:base_url + 'assets/img/date-bgr.png',
                buttonImageOnly:true
            });

        }

        // Custom function to enable one only day in jquery calender
        function enableDaysOfWeek(date) {

            var day = date.getDay();
            cond = false;

            for (var k in cur_days) {

                if (day == cur_days[k]) {
                    cond = true;
                }

            }

            //console.log(cond)
            return [(cond)];

        }

        /***********************************************************
         * Calculator
         ***********************************************************/
        $('#adults').live('change', function () {
            calculate();
        });
        $('#children').live('change', function () {
            calculate();
        });

        function calculate() {

            var adults = $('#adults').val() * adults_price;
            var children = $('#children').val() * children_price;
            $('#total-price').html('<span>Total: </span>' + (parseInt(adults) + parseInt(children)) + '€');

            // set input hidden
            $('[name=totalprice]').val(adults + children);
        //$('[name=adultprice]').val($('#adult-price').text());
        //$('[name=chprice]').val($('#children-price').text());

        }

        /***********************************************************
         * Confirm Availability
         ***********************************************************/
        $('.confirm_availability').live('click', function () {
            //that = jQuery(this).parent().busy({ img : p })
            if (check()) {
                $('#excusrsionsBook').submit();
            } else {
                return false;
            }

        });

        function check() {

            if (selected_date == undefined) {
                alert('Plese select a day.');
                return false;
            }

            if ($('#location').val() == 0) {
                alert("Please select pickup location.");
                return false;
            }


            if ($('#total-price').text() == '0') {
                alert('plese select al lest one person!');
                return false;
            }

            if ($('#adults').val() == 0) {
                alert("Please select at least one adult person.");
                return false;
            }


            return true;
        }

        $('#exc_date').val('');
        $('#exc_date').live('click', function () {
            $('#exc_date').blur();
        });

        /*$('#adults').val(2);
        $('#children').val(0);*/

        cal_init();

        calculate();


    }

    // Search excursions
    $('#changeDetasilsButton').live('click', function () {

        $('#changeDetasilsBox').hide();
        $('#serchModule').show();
        // Custom combox
        //$('.dropdownized').dropdownized();
        return false;

    })

    /***********************************************************
     * Search submit with fake form
     ***********************************************************/
    $('.submit_search').live('click', function () {
        var url = jQuery(this).attr('href');
        $('#submit_search').attr('action',url);
        $('#submit_search').submit();
        return false;
    });
    
    $(".tooltip").tooltip({
        show: {
            delay: 250
        },
        track: true
    }); 

	$('.search-list .car-box .details p').trunk8({ lines: 2});
	$('.car-box h4').trunk8({ lines: 2});
});

$(window).load(function() {
	$('.search-list .car-box .details p').trunk8({ lines: 2});
	$('.car-box h4').trunk8({ lines: 2});
});